package com.ifreeee.practice.pc.famousWorks;

import java.util.Objects;

/**
 *
 */
public class SerialNoGen {



    private StringBuilder sb = new StringBuilder();

    /**
     * 当前序号
     */
    private Integer currentNo;

    /**
     * 位数 (默认 6 位数应该够了)
     */
    private Integer noDigit;

    /**
     * 补充字符串
     */
    private Character fill;

    public SerialNoGen() {
        this.currentNo = 0;
        this.noDigit = 4;
        this.fill = '0';
    }

    public SerialNoGen(Integer currentNo) {
        this ();
        if (currentNo == null || currentNo < 0) {
            return;
        }
        this.currentNo = currentNo;
    }

    public void setNoDigit(Integer noDigit) {
        this.noDigit = noDigit;
    }

    public void setFill(Character fill) {
        this.fill = fill;
    }

    public String get() {
        String resNo = Objects.toString(currentNo++);
        Integer length = noDigit - resNo.length();
        if (length < 1) {
            return resNo;
        }
        sb.delete(0, sb.length());
        for (int i = 0; i < length; i++) {
            sb.append(this.fill);
        }
        sb.append(resNo);
        resNo = sb.toString();
        sb.delete(0, sb.length());
        return resNo;
    }

    public static void main(String[] args) {
        SerialNoGen serialNoGen = new SerialNoGen(1);
        System.out.println(serialNoGen.get());
        System.out.println(serialNoGen.get());
        System.out.println(serialNoGen.get());
        System.out.println(serialNoGen.get());
        System.out.println(serialNoGen.get());
        System.out.println(serialNoGen.get());
        System.out.println(serialNoGen.get());
        System.out.println(serialNoGen.get());
        System.out.println(serialNoGen.get());
        System.out.println(serialNoGen.get());
        System.out.println(serialNoGen.get());
        System.out.println(serialNoGen.get());
        System.out.println(serialNoGen.get());
        System.out.println(serialNoGen.get());
        System.out.println(serialNoGen.get());
    }

}
